import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_iot_plugin/pigeon.dart';

class FlutterIotPlugin implements FlutterAliLink {
  bool isSetup = false;
  static FlutterIotPlugin instance = FlutterIotPlugin._();

  factory FlutterIotPlugin() => instance;

  FlutterIotPlugin._();

  HostAliLink api = HostAliLink();

  void _setUp() {
    if (!isSetup) {
      FlutterAliLink.setup(FlutterIotPlugin.instance);
      isSetup = true;
    }
  }

  StreamController<String> iotResultStreamController = StreamController();

  FlutterAliLink? addDeviceListener;

  /// Flutter调用原生
  void startDiscovery() {
    _setUp();
    api.startDiscovery();
  }

  void stopDiscovery() {
    _setUp();
    api.stopDiscovery();
  }

  void startAddDeviceByBroadcast(String productKey, String productId) {
    _setUp();
    api.startAddDeviceByBroadcast(productKey, productId);
  }

  /// 热点配网
  void startAddDeviceBySoftAP(String productKey, String productId) {
    _setUp();
    api.startAddDeviceBySoftAP(productKey, productId);
  }

  ///蓝牙配网
  void startAddDeviceByBLE(String productKey, String productId) {
    _setUp();
    api.startAddDeviceByBLE(productKey, productId);
  }

  ///停止配网
  void stopAddDevice() {
    api.stopAddDevice();
  }

  ///设置wifi账号密码
  void toggleProvision(String ssid, String password, int timeout) {
    api.toggleProvision(ssid, password, timeout);
  }

  ///成功就可以获取token
  void getDeviceToken(String deviceName, String productKey, int timeout, int interval) {
    _setUp();
    api.getDeviceToken(deviceName, productKey, timeout, interval);
  }

  void getIotId(String token) {
    _setUp();
    api.getIotId(token);
  }

  ///登录后鉴权
  void authCodeLogin(String authCode) {
    _setUp();
    api.authCodeLogin(authCode);
  }

  Future<OtaNewVersionInfo?> checkOtaUpgradeInfo(String iotId) async {
    return api.checkOtaUpgradeInfo(iotId);
  }

  Future<int?> startOta(String iotId) async {
    return api.startOta(iotId);
  }

  Future<OtaUpgradeProgress?> getOtaProgress(String iotId) async {
    return api.getOtaProgress(iotId);
  }

  /// 原生调用Flutter

  @override
  void onPreCheck(int result, DCErrorCode? dcErrorCode) {
    print("1、onPreCheck,result=$result,dcErrorCode=$dcErrorCode");
    addDeviceListener?.onPreCheck(result, dcErrorCode);
  }

  @override
  void onProvisionPrepare(int prepareType) {
    print("2、onProvisionPrepare");
    addDeviceListener?.onProvisionPrepare(prepareType);
  }

  @override
  void onProvisioning() {
    print("3、onProvisioning");
    addDeviceListener?.onProvisioning();
  }

  @override
  void onProvisionStatus(int code, String msg) {
    print("4、onProvisionStatus,code=$code,msg=$msg");
    addDeviceListener?.onProvisionStatus(code, msg);
  }

  @override
  void onProvisionedResult(int success, DeviceInfo? deviceInfo, DCErrorCode? dcErrorCode) {
    print("5、onProvisionedResult,success=$success,deviceInfo=$deviceInfo,dcErrorCode=$dcErrorCode");
    addDeviceListener?.onProvisionedResult(success, deviceInfo, dcErrorCode);
  }

  @override
  void onDeviceFound(int type, String? description, List<DeviceInfo?>? deviceInfos) {
    print("5、onDeviceFound,type=$type,description=$description,deviceInfos length=${deviceInfos?.length}");
    addDeviceListener?.onDeviceFound(type, description, deviceInfos);
  }

  @override
  void getDeviceTokenOnFail(DCErrorCode dcErrorCode) {
    print("6、getDeviceTokenOnFail,dcErrorCode=$dcErrorCode");
    addDeviceListener?.getDeviceTokenOnFail(dcErrorCode);
  }

  @override
  void getDeviceTokenOnSuccess(GetTokenResult getTokenResult) {
    print("6、getDeviceTokenOnSuccess,getTokenResult=$getTokenResult");
    addDeviceListener?.getDeviceTokenOnSuccess(getTokenResult);
  }

  @override
  void onAuthCodeLoginFailed(int code, String msg) {
    print("7、onAuthCodeLoginFailed,code=$code,msg=$msg,addDeviceListener=$addDeviceListener");
    addDeviceListener?.onAuthCodeLoginFailed(code, msg);
  }

  @override
  void onAuthCodeLoginSuccess() {
    print("8、onAuthCodeLoginSuccess,addDeviceListener=$addDeviceListener");
    addDeviceListener?.onAuthCodeLoginSuccess();
  }

  @override
  void onGetIotIdResult(int result, String? iotId) {
    addDeviceListener?.onGetIotIdResult(result, iotId);
  }
}
